package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSArray;
import czh.cocoa.foundation.NSMutableArray;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSSelector;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIScreen;
import czh.cocoa.ui.UIView;

import static czh.cocoa.ui.impl.UIControl.*;

public class UITabBar extends UIView {

	private NSMutableArray mItemViews;
	
	private NSArray mItems;
	
	private UITabBarItem mSelectedItem;
	
	private UIView mIndicatorView;
	
	private UITabBarDelegate mDelegate;
	
	public UITabBar() {
		
	}
	
	protected void init() {
		super.init();
		mItemViews = new NSMutableArray(4);
		
		UIImage background = UIImage.imageNamed(NSTR("cocoa/tabbar"));
		
		float bar_width = UIScreen.mainScreen().bounds().size.width;
		float bar_height = DIP(background.size().height);
		setFrame(CGRectMake(0, 0, bar_width, bar_height));
		
		UIImageView bgview = new UIImageView(bounds());
		bgview.setImage(background);  
		bgview.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
		addSubview(bgview);
	
		UIImage indicator = UIImage.imageNamed(NSTR("cocoa/tabbar_selection"));
		UIImageView indicator_view = new UIImageView(indicator);
		indicator_view.setContentStretch(CGRectMake(0.5f, 0.5f, 0.1f, 0.1f)); 
		indicator_view.setHidden(true);
		addSubview(indicator_view);
		
		mIndicatorView = indicator_view;
	}
	
	public void setDelegate(UITabBarDelegate delegate) {
		mDelegate = delegate;
	}
	
	public UITabBarDelegate delegate() {
		return mDelegate;
	}
	
	public void setItems(NSArray items) {
		mItems = items;
		
		for (NSObject o : mItemViews) {
			UIView item_view = (UIView) o;
			item_view.removeFromSuperview();
		}
		mItemViews.removeAllObjects();
		
		if (items == null) {
			return;
		}
		
		int count = items.count();
		if (count > 0) {
			CGSize barsize = bounds().size;
			float item_width = barsize.width / count;
			for (int i = 0; i < count; i++) {
				UIButton barbtn = new UIButton();
				CGRect frame = CGRectMake(item_width * i, 0, item_width, barsize.height);
				barbtn.setFrame(frame);
				
				UIColor color = UIColor.colorWithWhiteAlpha(153/255.0f, 1);
				
				final UITabBarItem item = (UITabBarItem)items.objectAtIndex(i);
				UIImage image = item.image();
				if (image != null) {
					image = UIImage.imageWithBlendColor(image, color);
					float scale = image.size().height / (barsize.height * 0.5f);
					image = new UIImage(image.CGImage(), scale);
				}
				barbtn.setImageForState(image, UIControlStateNormal);
				barbtn.setTitleForState(item.title(), UIControlStateNormal);
				barbtn.setTitleColorForState(color, UIControlStateNormal);
				
				barbtn.setImageEdgeInsets(UIEdgeInsetsMake(0, 0, barsize.height * 0.3f, 0));
				barbtn.setTitleEdgeInsets(UIEdgeInsetsMake(barsize.height * 0.7f, 0, 0, 0));
				
				barbtn.titleLabel().setFont(UIFont.boldSystemFontOfSize(16)); 
				
				barbtn.addTargetActionForControlEvents(this, new NSSelector() {
					@Override
					public void perform(NSObject receiver, NSObject params) {
						setSelectedItem(item);
					}
				}, UIControlEventTouchUpInside);
				
				addSubview(barbtn);
				mItemViews.addObject(barbtn);
			}
			
			bringSubviewToFront(mIndicatorView);
		}
		
	}
	
	public NSArray items() {
		return mItems;
	}
	
	public void setSelectedItem(UITabBarItem item) {
		if (mSelectedItem != item) {
			int index = mItems.indexOfObject(item);
			if (index != NSNotFound) {
				UIButton barbtn = (UIButton) mItemViews.objectAtIndex(index);
				UIImage image = barbtn.imageForState(UIControlStateNormal);
				if (image != null) {
					UIColor color = new UIColor(50/255f, 169/255f, 246/255f, 1);
					image = UIImage.imageWithBlendColor(image, color);
					barbtn.setImageForState(image, UIControlStateNormal);
				}
				barbtn.setTitleColorForState(UIColor.whiteColor(), UIControlStateNormal);
				mIndicatorView.setFrame(barbtn.frame());
				mIndicatorView.setHidden(false);
			} else {
				mIndicatorView.setHidden(true);
			}
			if (mSelectedItem != null) {
				index = mItems.indexOfObject(mSelectedItem);
				UIButton barbtn = (UIButton) mItemViews.objectAtIndex(index);
				UIColor color = UIColor.colorWithWhiteAlpha(153/255.0f, 1);
				UIImage image = barbtn.imageForState(UIControlStateNormal);
				if (image != null) {
					image = UIImage.imageWithBlendColor(image, color);
					barbtn.setImageForState(image, UIControlStateNormal);
				}
				barbtn.setTitleColorForState(color, UIControlStateNormal);
			}
			
			mSelectedItem = item;
			
			if (mDelegate != null) {
				mDelegate.tabBarDidSelectItem(this, item);
			}
		}
	}
	
	public UITabBarItem selectedItem() {
		return mSelectedItem;
	}
	
	
}
